import random
import requests
import time

# 定义操作符列表
operators = ["大于", "小于", "等于", "大于等于", "小于等于"]

# 生成随机参数
def generate_random_condition():
    operator = random.choice(operators)
    data_value = round(random.uniform(0, 200), 2)  # 生成 0~200 的随机浮点数
    return {
        "operator": operator,
        "dataValue": data_value
    }

# 调用接口
def call_save_api(condition_data):
    url = "http://localhost:8080/condition/save"
    headers = {
        "Content-Type": "application/json"
    }
    try:
        response = requests.post(url, json=condition_data, headers=headers)
        return response.status_code, response.text
    except Exception as e:
        return 500, str(e)

if __name__ == "__main__":
    total = 500
    success_count = 0
    fail_count = 0

    print(f"开始循环调用 /condition/save 接口 {total} 次...\n")

    for i in range(1, total + 1):
        condition = generate_random_condition()

        status_code, response_text = call_save_api(condition)

        if status_code == 200:
            success_count += 1
            print(f"[{i}/{total}] 成功 | 状态码: {status_code} | 请求参数: {condition}")
        else:
            fail_count += 1
            print(f"[{i}/{total}] 失败 | 状态码: {status_code} | 错误信息: {response_text}")

        # 可选：每请求一次暂停一小段时间（防止服务器压力过大）
        time.sleep(0.05)

    print("\n调用完成！")
    print(f"成功次数: {success_count}")
    print(f"失败次数: {fail_count}")
